<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js动态脚本语法参详</title>
</head>
<style>
    div {margin: 10px}
    i{background-color: #9E9E9E}
    b{font-size: 14px;}
    h3{border: blue 1px dashed; }
</style>
<script type="text/javascript">

    function showJson(json){
        return "<pre>"+syntaxHighlight(json)+"</pre>"
    }

    // 方法实现
    function syntaxHighlight(json) {
        if (typeof json != 'string') {
            json = JSON.stringify(json, undefined, 2); //返回要JSON化的对象，2是spacing
        }
        json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
        return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
            function (match) {
                var cls = 'number';
                if (/^"/.test(match)) {
                    if (/:$/.test(match)) {
                        cls = 'key';
                    } else {
                        cls = 'string';
                    }
                } else if (/true|false/.test(match)) {
                    cls = 'boolean';
                } else if (/null/.test(match)) {
                    cls = 'null';
                }
                return '<span style="color: green;">' + match + '</span>';
            }
        );
    }
</script>
<body>
<h1>支持js es5语法,参考函数如下</h1>
<p style="color: #9E9E9E">采用java nashorn script engine,进行解析!支持原生JSON.parse等之类的部分函数。</p>
<div>
    <h3>游标方式流式sql读取处理<span style="color: red">【废弃,建议用streamSql2】</span></h3>
    <p>
        api.streamSql(String sql,Object[] ps,ScriptObjectMirror objectMirror)
    </p>
    <i>api.streamSql("select * from tb_sample_2022_09_15 where url=?",["http://localhost:8081/test4"],function (dataMap){<br/>
    //todo <br/>
    })</i>
</div>
<div>
    <h3>根据id批量流式sql读取处理</h3>
    <p>
        api.streamSql2(String sql,Object[] ps,ScriptObjectMirror objectMirror)
    </p>
    <i>api.streamSql2("select * from tb_sample_2022_09_15 where url=?",["http://localhost:8081/test4"],function (dataMap){<br/>
        //todo <br/>
        })</i>
</div>
<div>
<h3>样本文件写入</h3>
    <p>
        api.writeSample(Object sample)
    </p>
    <i>api.writeSample({a=1,b=""})</i>
</div>
<div>
<h3>日志打印</h3>
    <p>
        api.log(Object info)
    </p>
    <i>api.log("输出一段文字到日志中...")</i>
</div>
<h3>错误打印</h3>
<p>
    api.error(Object info)
</p>
<i>api.error("输出一段文字到日志中...")</i>
<h3>调试打印</h3>
<p>
    api.debug(Object info)
</p>
<i>api.debug("只显示到node的本地日志中,无法从web控制台上看到...")</i>
</div>
<div>
    <h3>当前时间格式化</h3>
    <p>
        api.nowFormat(String format)
    </p>
    <i>api.nowFormat("yyyy-MM-dd")</i>
</div>
<div>
    <h3>动态参数</h3>
    <p>
        api.ps.?
    </p>
    <b>通用参数: report</b>
    <p>report对象:
        <script type="text/javascript">
            document.write(showJson({"id":229,"report_name":"测试_2022_10_01_22_52_19","tran_id":"2022_10_01_22_52_19","task_id":2,"nodes":"LAPTOP-7V5D9SQE","nodes_info":"[{\"node\":\"LAPTOP-7V5D9SQE\",\"cpu\":8,\"memory\":16048,\"threads\":1}]","filter_table":"2022_10_01_22_52_19-2.sample","filter_store":"mysql","begin_time":"2022-10-01 22:52:19","end_time":"1900-01-01 00:00:00","create_time":"2022-10-01 22:52:19","report_node_table":"auto_tb_report_node_2022_10_01_22_52_19","report_url_table":"auto_tb_report_url_2022_10_01_22_52_19","task_name":"测试","filter_table_lines":0,"filter_table_error_lines":0}
            ));
        </script>
    </p>
    <b>通用参数: task</b>
    <p>task对象:
        <script type="text/javascript">
            document.write(showJson({
                "id": 1,
                "task": "111",
                "filter_store": "mysql",
                "run_heart_time": "2022-09-22 13:56:42",
                "create_user": "admin",
                "create_time": "2022-09-22 13:56:42",
                "update_time": "2022-09-22 13:56:42",
                "update_user": "admin",
                "filter_script": "",
                "filter_table": "aaa.sample",
                "clear_data_first": true,
                "nodes": "test",
                "run_threads_count": 1,
                "http_begin_script": "",
                "http_end_script": "",
                "check_stop_script": ""
            }));
        </script>
    </p>
    <b>通用参数: node</b>
    <p>node对象:
        <script type="text/javascript">
            document.write(showJson({"id":17,"node":"LAPTOP-7V5D9SQE","cpu":0.0,"memory":602.0,"threads":0,"ip":"10.8.0.6","heatbeat_time":"2022-10-04 10:39:27","port":"8081","local_cpu":8,"local_memory":16048,"used":true}));
        </script>
    </p>
    <b>通用参数: index</b>
    <p>index对象:表示动态启动节点时,节点所分配的节点集合启动位置;从0开始,最大不超过当前节点数-1;
    </p>
    <b>通用参数: params</b>
    <p>params对象:任务动态传入的参数,一般用于定时计划,动态传参!
        <script type="text/javascript">
            document.write(showJson({"aaa":"test","param2":"参数2"}));
        </script>
    </p>
    <b>第一次过滤错误样本脚本: 通用参数,request,response,返回是否继续执行(true/false)</b>
    <p>request对象:
        <script type="text/javascript">
            document.write(showJson({
                "id": 2,
                "url": "http://192.168.8.73:8700/bsf/health/",
                "app_name": "lmc-test-provider",
                "header": "{\"aaa\": \"1\", \"host\": \"localhost:8081\", \"accept\": \"*/*\", \"connection\": \"keep-alive\", \"user-agent\": \"ApiPOST Runtime +https://www.apipost.cn\"}",
                "body": "{\"str\":\"bbbb\"}",
                "create_time": "2022-09-15 03:27:19",
                "fromip": "10.8.0.6",
                "traceid": "lmc-test-provider-9a5980af177148e29b63070b0701cc16",
                "trace_top": "是",
                "method": "POST",
                "operator_type": "操作"
            }));
        </script>
    </p>
    <p>response对象:
        <script type="text/javascript">
            document.write(showJson({
                "request": {
                    "appName": "lmc-test-provider",
                    "httpUrl": "http://192.168.8.73:8700/bsf/health/",
                    "method": "POST",
                    "header": {
                        "aaa": "1",
                        "content-length": "14",
                        "accept-language": "zh-CN",
                        "host": "localhost:8081",
                        "connection": "keep-alive",
                        "content-type": "application/json",
                        "accept-encoding": "gzip, deflate, br",
                        "accept": "*/*",
                        "user-agent": "ApiPOST Runtime +https://www.apipost.cn"
                    },
                    "body": "{\"str\":\"bbbb\"}"
                },
                "code": 200,
                "method": "POST",
                "header": {
                    "Transfer-Encoding": "chunked",
                    "": "HTTP/1.1 200",
                    "Date": "Thu, 22 Sep 2022 10:31:56 GMT",
                    "Content-Type": "application/json;charset=UTF-8"
                },
                "body": "文本输出....",
                "requestSize": 14,
                "responseSize": 13792,
                "timeMs": 47401,
                "success": true
            }));
        </script>
    </p>
    <b>执行前脚本参数:  通用参数,request,返回是否继续执行(true/false)</b>
    <p>request对象:
        <script type="text/javascript">
            document.write(showJson({
                "id": 2,
                "url": "http://192.168.8.73:8700/bsf/health/",
                "app_name": "lmc-test-provider",
                "header": "{\"aaa\": \"1\", \"host\": \"localhost:8081\", \"accept\": \"*/*\", \"connection\": \"keep-alive\", \"user-agent\": \"ApiPOST Runtime +https://www.apipost.cn\"}",
                "body": "{\"str\":\"bbbb\"}",
                "create_time": "2022-09-15 03:27:19",
                "fromip": "10.8.0.6",
                "traceid": "lmc-test-provider-9a5980af177148e29b63070b0701cc16",
                "trace_top": "是",
                "method": "POST",
                "operator_type": "操作"
            }));
        </script>
    </p>
    <b>执行后脚本参数: 通用参数, request,response,返回是否继续执行(true/false)</b>
    <p>request对象:
        <script type="text/javascript">
            document.write(showJson({
                "id": 2,
                "url": "http://192.168.8.73:8700/bsf/health/",
                "app_name": "lmc-test-provider",
                "header": "{\"aaa\": \"1\", \"host\": \"localhost:8081\", \"accept\": \"*/*\", \"connection\": \"keep-alive\", \"user-agent\": \"ApiPOST Runtime +https://www.apipost.cn\"}",
                "body": "{\"str\":\"bbbb\"}",
                "create_time": "2022-09-15 03:27:19",
                "fromip": "10.8.0.6",
                "traceid": "lmc-test-provider-9a5980af177148e29b63070b0701cc16",
                "trace_top": "是",
                "method": "POST",
                "operator_type": "操作"
            }));
        </script>
    </p>
    <p>response对象:
        <script type="text/javascript">
            document.write(showJson({
                "request": {
                    "appName": "lmc-test-provider",
                    "httpUrl": "http://192.168.8.73:8700/bsf/health/",
                    "method": "POST",
                    "header": {
                        "aaa": "1",
                        "content-length": "14",
                        "accept-language": "zh-CN",
                        "host": "localhost:8081",
                        "connection": "keep-alive",
                        "content-type": "application/json",
                        "accept-encoding": "gzip, deflate, br",
                        "accept": "*/*",
                        "user-agent": "ApiPOST Runtime +https://www.apipost.cn"
                    },
                    "body": "{\"str\":\"bbbb\"}"
                },
                "code": 200,
                "method": "POST",
                "header": {
                    "Transfer-Encoding": "chunked",
                    "": "HTTP/1.1 200",
                    "Date": "Thu, 22 Sep 2022 10:31:56 GMT",
                    "Content-Type": "application/json;charset=UTF-8"
                },
                "body": "文本输出....",
                "requestSize": 14,
                "responseSize": 13792,
                "timeMs": 47401,
                "success": true
            }));
        </script>
    </p>
    <b>结束规则脚本参数: 通用参数, nodeReport,runtime,返回是否继续执行(true/false)</b>
    <p>runtime:运行时间(单位:秒)</p>
    <p>nodeReport:初始化时为null<br/>
    </p>
    <p>nodeReport对象:
        <script type="text/javascript">
            document.write(showJson({
                "node": "test",
                "cpu": 0.008473612979373302,
                "memory": 343.0,
                "network_read": 3485918.0,
                "network_write": 2128.0,
                "active_threads": 114,
                "throughput": 266,
                "error": 0,
                "create_time": "2022-09-22 18:51:15",
                "timeSpan": 46.0
            }));
        </script>
    </p>
    <i>api.ps.task,api.ps.report,.....</i>
</div>
<div>
    <h3>睡眠n毫秒</h3>
    <p>
        api.sleep(Integer time),谨慎使用!
    </p>
    <i>api.sleep(5)</i>
</div>
<div>
    <h3>http Post Json请求支持,返回String</h3>
    <p>
        api.httpPost(String url,Object json)
    </p>
    <i>var result=api.httpPost("http://www.baidu.com/",{"aa":111})</i>
</div>
<div>
    <h3>http Post Form请求支持,返回String</h3>
    <p>
        api.httpPostForm(String url,Object form)
    </p>
    <i>无,注意:没测试过,不建议使用</i>
</div>
<div>
    <h3>http Get请求支持,返回String</h3>
    <p>
        api.httpGet(String url)
    </p>
    <i>var result=api.httpGet("http://wwww.baidu.com/")</i>
</div>
</body>
</html>